package sidconverter.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.SimpleAttributeSet;

import emulator.GUI.PersistentFrame;
import emulator.shell.GUI.ConsoleOutputStream;
import emulator.shell.GUI.ConsoleWriteEvent;
import emulator.shell.GUI.LogPanel;

@SuppressWarnings("serial")
public class AnalyzerFrame extends PersistentFrame implements LogPanel {

	private JPanel contentPane;
	private JTextPane content;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnalyzerFrame frame = new AnalyzerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param parent 
	 */
	public AnalyzerFrame() {
		super("AnalyzerFrame", "SID Converter");
		setTitle("SID Analysis");
		setBounds(100, 100, 500, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onClose();
			}
		});
		contentPane.add(btnOk, BorderLayout.SOUTH);
		
		content = new JTextPane();
		content.setBorder(new TitledBorder(null, "Log", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		content.setEditable(false);
		contentPane.add(new JScrollPane(content), BorderLayout.CENTER);
	}

	protected void onClose() {
		savePosition();
		this.setVisible(false);
	}

	@Override
	public void write(String text, SimpleAttributeSet attributes) {
        javax.swing.SwingUtilities.invokeLater(new ConsoleWriteEvent(this, text, attributes));
	}

	@Override
	public JTextPane getContentFrame() {
		return content;
	}

	public PrintStream createOut() {
		OutputStream out = new ConsoleOutputStream(this, Color.gray, false);
		return new PrintStream(out);
	}
	
	public PrintStream createErr() {
		OutputStream err = new ConsoleOutputStream(this, Color.red, true);
		return new PrintStream(err);
	}
	
}
